<?php

// Librairie de fonction MYSQL
// Fonctions spécifiques à l'application de Libérations
// Date de début et de fin de la période courante
$datedebut = null;
$datefin = null;

// // Date de début et de fin de la période courante BER
//$datedebutber = null;
//$datefinber = null;
//Liste des employeurs
unset($donneeslisteemployeurs);

//Liste des employes
unset($donneeslisteemployes);

function getPeriodeCourante($param = '') {
    //TODO remplacer les variables globales par des paramètres dans la fonction
    global $datedebut;
    global $datefin;

    if (!isset($datedebut) || !isset($datefin)) {
        $sqlparam["type"] = "SELECT";
        $sqlparam["table"][] = "compteurautre";
        $sqlparam["champs"][] = "renouvellement";
        $sqlparam["limit"][] = 1;

        $result = executerRequeteSql($sqlparam);
        $datefin = date("Y-m-d", strtotime("-1 day", strtotime($result[0]["renouvellement"])));
        $datedebut = date("Y-m-d", strtotime("-1 year + 1 day", strtotime($datefin)));
    }
    if ($param == "début") {
        return $datedebut;
    } elseif ($param == "fin") {
        return $datefin;
    } else {
        return array("datedebut" => $datedebut, "datefin" => $datefin);
    }
}

function getEmployeurs($droit = '') {
    //TODO Enlever la variable $donneeslisteemployeurs
    global $donneeslisteemployeurs;
    if (!isset($donneeslisteemployeurs)) {
        $sqlparam["type"] = "SELECT";
        $sqlparam["table"][] = "employeurs";
        $sqlparam["champs"][] = "refemployeur as refid";
        $sqlparam["champs"][] = "nomemployeur as valeur";
        $sqlparam["ordre"][] = "nomemployeur ASC";

        if (isset($_SESSION[$droit]) && $_SESSION[$droit] != 'Oui') {
            $sqlparam["where"][] = sprintf("refemployeur=%d", $_SESSION["employeur"]);
        }
        $donneeslisteemployeurs = executerRequeteSql($sqlparam);
    }
    return $donneeslisteemployeurs;
}

function getEmployes($employeur = '', $droit = '', $tous_les_employes = false) {
    //TODO Enlever la variable $donneeslisteemployes
    global $donneeslisteemployes;
    if (($employeur != '' && is_numeric($employeur)) || ($droit != '' && $droit == 'gestion_site' && $_SESSION[$droit] != 'Oui')) {
        $sqlparam["where"][] = sprintf("employes.refemployeur = %d", $employeur);
    }
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "employes";
    $sqlparam["champs"][] = "refemploye as refid";
    $sqlparam["champs"][] = "CONCAT(IFNULL(employes.nomfamille,''), ', ', IFNULL(employes.prenom,' '),',"
            . " ', IFNULL(employes.adresse1,' '),', ', IFNULL(employes.ville,' ')) as valeur";
    $sqlparam["ordre"][] = "CONCAT(employes.nomfamille,employes.prenom)";
    // Tenir compte des employés actif ou non
    $sqlparam["where"][] = "archive=0";
    return executerRequeteSql($sqlparam);
}

function getLieuxTravail($droit = '') {
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "lieuxtravail";
    $sqlparam["table"][] = "employeurs";
    $sqlparam["join"][] = "employeurs.refemployeur = lieuxtravail.refemployeur";
    $sqlparam["champs"][] = "lieuxtravail.reflieutravail as refid";
    $sqlparam["champs"][] = "lieuxtravail.nomlieutravail as valeur";
    $sqlparam["champs"][] = "employeurs.nomemployeur as nomemployeur";
    $sqlparam["ordre"][] = "lieuxtravail.reflieutravail ASC";

    if (isset($_SESSION[$droit]) && $_SESSION[$droit] != 'Oui') {
        $sqlparam["where"][] = sprintf("reflieutravail=%d", $_SESSION["lieutravail"]);
    }

    return executerRequeteSql($sqlparam);
}

function getFournisseursCellulaire() {
    $sqlparam["connexion"] = "maitre";
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "fournisseurs_cellulaire";
    $sqlparam["champs"][] = "format as refid";
    $sqlparam["champs"][] = "nom as valeur";
    $sqlparam["ordre"][] = "nom ASC";

    return executerRequeteSql($sqlparam);
}
